package net.sddf.system.bank.business.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.sddf.system.bank.business.BorrowRecordService;
import net.sddf.system.bank.business.model.dto.BorrowRecordStatusBankDTO;
import net.sddf.system.bank.business.model.vo.BorrowRecordStatusBankVO;
import net.sddf.system.bank.business.model.vo.BorrowRecordVo;
import net.sddf.system.bank.common.constant.BorrowRecordStatus;
import net.sddf.system.bank.common.util.EncryptUtil;
import net.sddf.system.bank.entity.BorrowRecord;
import net.sddf.system.bank.mapper.BorrowRecordMapper;
import net.sddf.system.bank.model.CreateProjectResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class BorrowRecordServiceImpl extends ServiceImpl<BorrowRecordMapper, BorrowRecord> implements BorrowRecordService {

    @Override
    public CreateProjectResponse syncBorrowRecord(String reqData) {
        String decodeUrlData = EncryptUtil.decodeURL(reqData);
        String decodeReqData = EncryptUtil.decodeUTF8StringBase64(decodeUrlData);
        log.info("银行托管，decodeReqData：" + decodeReqData);
        BorrowRecordVo borrowRecordVo = JSON.parseObject(decodeReqData, BorrowRecordVo.class);
        log.info("银行托管，borrowRecordVo：" + borrowRecordVo);
        String requestNo = borrowRecordVo.getReqCode();

        // 保存借款项目信息
        BorrowRecord borrowRecord = new BorrowRecord();
        BeanUtils.copyProperties(borrowRecordVo, borrowRecord);
        borrowRecord.setName(borrowRecordVo.getName());
        // borrowRecord.setDescription();
        // borrowRecord.setType(borrowRecordVo.get());
        borrowRecord.setPeriod(borrowRecordVo.getPeriod());
        // borrowRecord.setBorrowerAnnualRate(borrowRecordVo.get());
        borrowRecord.setAmount(borrowRecordVo.getAmount());
        borrowRecord.setBorrowRecordNo(borrowRecordVo.getBorrowRecordNo());
        borrowRecord.setBorrowStatus(BorrowRecordStatus.READY.getCode());
        save(borrowRecord);

        return new CreateProjectResponse(requestNo);
    }

    @Override
    public BorrowRecordStatusBankVO updateBorrowRecordStatus(String reqData) {
        String decodeUrlData = EncryptUtil.decodeURL(reqData);
        String decodeReqData = EncryptUtil.decodeUTF8StringBase64(decodeUrlData);
        BorrowRecordStatusBankDTO brsDTO = JSON.parseObject(
                decodeReqData, BorrowRecordStatusBankDTO.class);
        log.info("银行托管系统-更改借款状态，请求数据：{}", brsDTO);

        update(Wrappers.<BorrowRecord>update().lambda().eq(
                        BorrowRecord::getBorrowRecordNo, brsDTO.getBorrowRecordNo())
                .set(BorrowRecord::getBorrowStatus, brsDTO.getBorrowStatus()));
        log.info("银行托管系统-更改借款状态，已修改为：{}", brsDTO.getBorrowStatus());

        return BorrowRecordStatusBankVO.builder()
                .borrowStatus(brsDTO.getBorrowStatus())
                .reqCode(brsDTO.getReqCode())
                .build();
    }

    @Override
    public BorrowRecord getByProjectNo(String projectNo) {
        return getOne(Wrappers.<BorrowRecord>query().lambda().eq(BorrowRecord::getBorrowRecordNo, projectNo), false);
    }
}
